package cn.edu.hubu.hubusmartmedicalsystem.service.impl;

import cn.edu.hubu.hubusmartmedicalsystem.entity.Report;
import cn.edu.hubu.hubusmartmedicalsystem.mapper.ReportMapper;
import cn.edu.hubu.hubusmartmedicalsystem.service.ReportService;
import cn.edu.hubu.hubusmartmedicalsystem.vo.ResultVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ReportServiceImpl implements ReportService {

    private static final Logger logger = LoggerFactory.getLogger(ReportServiceImpl.class);

    @Autowired
    private ReportMapper reportMapper;

    @Override
    public ResultVO<Report> getReportById(String reportId) {
        if (reportId == null || reportId.length() != 10) {
            return new ResultVO<>(400, "报告ID必须是10个字符", null);
        }

        Report report = reportMapper.selectById(reportId);
        if (report == null) {
            return new ResultVO<>(404, "报告不存在", null);
        }
        return new ResultVO<>(200, "查询成功", report);
    }

    @Override
    public ResultVO<List<Report>> getAllReports() {
        List<Report> reports = reportMapper.selectAll();
        return reports.isEmpty() ?
                new ResultVO<>(404, "没有找到报告", null) :
                new ResultVO<>(200, "查询成功", reports);
    }

    @Override
    public ResultVO<String> addReport(Report report) {
        // 校验 registe_id 长度
        if (report.getRegisteId() == null || report.getRegisteId().length() != 10) {
            return new ResultVO<>(400, "registe_id 必须是 10 个字符", null);
        }
        if (reportMapper.selectById(report.getReportId()) != null){
            return new ResultVO<>(500,"添加失败，该报告已存在",null);
        }
        int result = reportMapper.insertReport(report);
        return result > 0 ?
                new ResultVO<>(200, "报告添加成功", null) :
                new ResultVO<>(500, "报告添加失败", null);
    }


    @Override
    public ResultVO<String> addReports(List<Report> reports) {
        // 校验每个报告的 registe_id 长度
        for (Report report : reports) {
            if (report.getRegisteId() == null || report.getRegisteId().length() != 10) {
                return new ResultVO<>(400, "registe_id 必须是 10 个字符: " + report.getRegisteId(), null);
            }
        }

        int result = reportMapper.insertReports(reports);
        return result > 0 ?
                new ResultVO<>(200, "批量报告添加成功", null) :
                new ResultVO<>(500, "批量报告添加失败", null);
    }


    @Override
    public ResultVO<String> updateReport(Report report) {
        int result = reportMapper.updateReport(report);
        return result > 0 ?
                new ResultVO<>(200, "报告更新成功", null) :
                new ResultVO<>(500, "报告更新失败", null);
    }

    @Override
    public ResultVO<String> deleteReport(String reportId) {
        int result = reportMapper.deleteById(reportId);
        return result > 0 ?
                new ResultVO<>(200, "报告删除成功", null) :
                new ResultVO<>(500, "报告删除失败", null);
    }

    @Override
    public ResultVO<String> deleteReports(List<String> reportIds) {
        if (reportIds == null || reportIds.isEmpty()) {
            return new ResultVO<>(400, "报告ID列表不能为空", null);
        }

        try {
            int result = reportMapper.deleteReports(reportIds); // 调用 Mapper 方法
            if (result > 0) {
                return new ResultVO<>(200, "批量删除报告成功", null);
            } else {
                return new ResultVO<>(404, "未找到需要删除的报告", null);
            }
        } catch (Exception e) {
            logger.error("批量删除报告时发生异常: {}", e.getMessage());
            return new ResultVO<>(500, "批量删除报告失败，发生异常", null);
        }
    }

}
